<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\DemoBundle\Entity\Chung;
use Acme\DemoBundle\Entity\Nhanvien;
use Acme\DemoBundle\Entity\Luong;


class TinhLuongController extends Controller
{
	public function getListPhongBan(){
		return $this->getDoctrine()
		->getRepository('AcmeDemoBundle:PhongBan')->findAllByCoditions();
	}
	public function danhsachAction()
	{
		$chung = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Chung')
		->find(1);
		$dateNow = new \DateTime('now');
		$yearNow = $dateNow->format("Y");
		$monthNow = $dateNow->format("m");
		$lstphongban = $this->getListPhongBan();
		return $this->render(
				'AcmeDemoBundle:Demo:tinhluong.html.twig',
				array('lstphongban' => $lstphongban,'yearNow'=>$yearNow,'monthNow'=>$monthNow,
						'luongcoban'=>number_format($chung->getLuongcoban()),'luongcvcoban'=>number_format($chung->getLuongcvcoban())
						,'luongbaohiem' => number_format($chung->getLuongbaohiem())
		)
		);
	}
	public function luuAction()
	{
		$request = $this->get('request');
		$lstTinhLuong = $request->request->get('myData');
		$nam = $request->request->get('nam');
		$thang = $request->request->get('thang');
		
		
		$lstDanhSach = json_decode($lstTinhLuong);
		$arr = $lstDanhSach["rows"];
		$items = array(); 
		foreach ($arr as $obj){
			$luong = new Luong();
			$luong->set
				if($obj->machucvu == "GDO"){
					
				}
		}
	}
	public function getdanhsachAction()
	{
		$chung = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Chung')
		->find(1);
		
		$phongban = $this->getListPhongBan();
		
		$request = $this->get('request');
	
		
		$type = $request->request->get('type') ? strval($request->request->get('type')) : "0";
		if($type=="0"){
			$phongbanid = $request->request->get('phongban') ? intval($request->request->get('phongban')) : $phongban[0]->getId();
			$lstNhanVien = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:NhanVien')
			->findByPhongBanId($phongbanid);
			
			
			$result = array();
			$result["total"] = \Count($lstNhanVien);
			
			$items = array();
			
			
			foreach ($lstNhanVien as $nhanvien){
					
				if($nhanvien->getTinhbaohiem() == "1"){
					$tinhbaohiem = "Có";
					$bhxh = $chung->getLuongbaohiem() * ($nhanvien->getMucluong() + $nhanvien->getChucvuid()->getHeso()) * $chung->getHesoBHXH();
					$bhyt = $chung->getLuongbaohiem() * ($nhanvien->getMucluong() + $nhanvien->getChucvuid()->getHeso()) * $chung->getHesoBHYT();
					$bhtn = $chung->getLuongbaohiem() * ($nhanvien->getMucluong() + $nhanvien->getChucvuid()->getHeso()) * $chung->getHesoBHTN();
				} else {
					$tinhbaohiem = "Không";
					$bhxh= 0;
					$bhyt= 0;
					$bhtn= 0;
				}
			
				if($nhanvien->getTinhantrua() == "1"){
					$tinhantrua = "Có";
					$tienantrua = $chung->getNgaytinhcong()*$chung->getTienantrua();
				} else {
					$tinhantrua = "Không";
					$tienantrua = 0;
				}
				$luong = $nhanvien->getMucluong()*$chung->getLuongcoban()*$chung->getNgaytinhcong()/$chung->getTongngaycong();
				$luongcongviec = $nhanvien->getHesoluongcv()*$chung->getLuongcvcoban()*$chung->getNgaytinhcong()/$chung->getTongngaycong();
				$phucapcv = $nhanvien->getChucvuid()->getHeso()*$chung->getLuongcoban();
				$thuclanh = $luong + $luongcongviec + $phucapcv + $tienantrua - $bhxh - $bhyt- $bhtn;
				$thuclanh1 = $thuclanh - $nhanvien->getGiamtrugiacanh();
				$luongsauthue = 0;
				if($thuclanh1>0){
						if($thuclanh1<=5000000){
							$luongsauthue = $thuclanh1*5/100;
						} elseif ($thuclanh1>5 && $thuclanh1<=10) {
							$luongbac1 = 250000;
							$luongsauthue = $thuclanh - $luongbac1 + ((10000000-$thuclanh1)*10/100);
								
						} elseif ($thuclanh1>10000000 && $thuclanh1<=18000000) {
							$luongbac1 = 250000;
							$luongbac2 = 500000;
							$luongsauthue = $luongbac1 + $luongbac2 + ((18000000-$thuclanh1)*15/100);
								
						} elseif ($thuclanh1>18000000 && $thuclanh1<=32000000) {
							$luongbac1 = 250000;
							$luongbac2 = 500000;
							$luongbac3 = 1200000;
							$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + ((32000000-$thuclanh1)*20/100);
								
						}elseif ($thuclanh1>32000000 && $thuclanh1<=35000000) {
							$luongbac1 = 250000;
							$luongbac2 = 500000;
							$luongbac3 = 1200000;
							$luongbac4 = 2800000;
							$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + ((35000000-$thuclanh1)*25/100);
								
						}elseif ($thuclanh1>52000000 && $thuclanh1<=80000000) {
							$luongbac1 = 250000;
							$luongbac2 = 500000;
							$luongbac3 = 1200000;
							$luongbac4 = 2800000;
							$luongbac5 = 5000000;
							$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + $luongbac5 + ((80000000-$thuclanh1)*30/100);
								
						}elseif ($thuclanh1>80000000) {
							$luongbac1 = 250000;
							$luongbac2 = 500000;
							$luongbac3 = 1200000;
							$luongbac4 = 2800000;
							$luongbac5 = 5000000;
							$luongbac6 = 8400000;
							$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + $luongbac5 + $luongbac6 + (($thuclanh1 - 80000000)*35/100);
						}
					}
				$item = array("id"=>$nhanvien->getId(),"hoten"=>$nhanvien->getHoten(),
						"mucluong"=>$nhanvien->getMucluong(),
						"machucvu"=>$nhanvien->getChucvuid()->getMa(),
						"chucvu"=>$nhanvien->getChucvuid()->getTen(),
						"mucphucap"=>$nhanvien->getChucvuid()->getHeso(),
						"tienphucap"=>number_format($phucapcv),
						"ngaycong"=>$chung->getNgaytinhcong(),
						"tienluongcoban"=> number_format($luong),
						
						"hesoluongcongviec"=>$nhanvien->getHesoluongcv(),
						"tienluongcongviec"=>number_format($luongcongviec),
						"luongkhuyenkhich"=>number_format($nhanvien->getTienphucap()),
						"ngaynghi"=>0,
						"tinhbaohiem"=>$tinhbaohiem,
						"tinhantrua"=>$tinhantrua,
						"lg"=>$nhanvien->getLg(),
						"bhxh"=>number_format($bhxh),
						"bhyt"=>number_format($bhyt),
						"bhtn"=>number_format($bhtn),
						"congle"=>0,
						"tienantrua"=>number_format($tienantrua),
						"ngaylamthem"=>0,
						"tienconglamthem"=>0,
						"chuthich" => "",
						"thuclanh"=>number_format($thuclanh),
						"giamtrugiacanh"=>number_format($nhanvien->getGiamtrugiacanh()),
						"luongsauthue"=>number_format(luongsauthue)
				);
			
				array_push($items, $item);
			}
			
			$result["rows"] = $items;
		} else {
			$data = $request->request->get('data');
			$lstDanhSach = json_decode($data);
			$count = 0;
			$tong = 0;
			$arr =  array();
			foreach ($lstDanhSach as $danhsach){
				$count++;
				if($count ==1){
					$tong = $danhsach;
				}
				if($count ==2){
					$arr = $danhsach;
				}
				
			}
			
			$result = array();
			$result["total"] =$tong;
			
			$items = array();
			foreach ($arr as $obj){
				if($obj->machucvu == "GDO"){
					$obj->tienluongcoban = number_format(15500000);
					$obj->tienluongcongviec = number_format(($chung->getLuongcoban() * $obj->hesoluongcongviec * $chung->getNgaytinhcong()/$chung->getTongngaycong())*$obj->lg);
				}
				if($obj->machucvu == "NVM"){
					$obj->tienluongcoban = number_format(2000000);
				}
				if($obj->ngaynghi != null && intval($obj->ngaynghi)>0){
					$obj->congle = number_format($chung->getLuongcoban() * intval($obj->ngaynghi) * $obj->mucluong/22);
				}
				$luong = intval(str_replace(',','',$obj->tienluongcoban));
				$luongcongviec = intval(str_replace(',','',$obj->tienluongcongviec));
				$phucapcv = intval(str_replace(',','',$obj->tienphucap));
				$tienantrua = intval(str_replace(',','',$obj->tienantrua));
				$bhxh = intval(str_replace(',','',$obj->bhxh));
				$bhyt = intval(str_replace(',','',$obj->bhyt));
				$bhtn = intval(str_replace(',','',$obj->bhtn));
				$congle = intval(str_replace(',','',$obj->congle));
				$thuclanh = $luong + $luongcongviec + $phucapcv +$congle+ $tienantrua - $bhxh - $bhyt- $bhtn;
				$thuclanh1 = $thuclanh - $nhanvien->getGiamtrugiacanh();
				$luongsauthue = 0;
				if($thuclanh1>0){
					if($thuclanh1<=5000000){
						$luongsauthue = $thuclanh1*5/100;
					} elseif ($thuclanh1>5 && $thuclanh1<=10) {
						$luongbac1 = 250000;
						$luongsauthue = $thuclanh - $luongbac1 + ((10000000-$thuclanh1)*10/100);
							
					} elseif ($thuclanh1>10000000 && $thuclanh1<=18000000) {
						$luongbac1 = 250000;
						$luongbac2 = 500000;
						$luongsauthue = $luongbac1 + $luongbac2 + ((18000000-$thuclanh1)*15/100);
							
					} elseif ($thuclanh1>18000000 && $thuclanh1<=32000000) {
						$luongbac1 = 250000;
						$luongbac2 = 500000;
						$luongbac3 = 1200000;
						$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + ((32000000-$thuclanh1)*20/100);
							
					}elseif ($thuclanh1>32000000 && $thuclanh1<=35000000) {
						$luongbac1 = 250000;
						$luongbac2 = 500000;
						$luongbac3 = 1200000;
						$luongbac4 = 2800000;
						$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + ((35000000-$thuclanh1)*25/100);
							
					}elseif ($thuclanh1>52000000 && $thuclanh1<=80000000) {
						$luongbac1 = 250000;
						$luongbac2 = 500000;
						$luongbac3 = 1200000;
						$luongbac4 = 2800000;
						$luongbac5 = 5000000;
						$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + $luongbac5 + ((80000000-$thuclanh1)*30/100);
							
					}elseif ($thuclanh1>80000000) {
						$luongbac1 = 250000;
						$luongbac2 = 500000;
						$luongbac3 = 1200000;
						$luongbac4 = 2800000;
						$luongbac5 = 5000000;
						$luongbac6 = 8400000;
						$luongsauthue = $luongbac1 + $luongbac2 + $luongbac3 + $luongbac4 + $luongbac5 + $luongbac6 + (($thuclanh1 - 80000000)*35/100);
					}
				}
				$obj->thuclanh = $thuclanh;
				$obj->luongsauthue = $luongsauthue;
				array_push($items, $obj);
			}
			$result["rows"] = $items;
		}
	
		
	
			
		$response = new Response(json_encode($result));
		$response->headers->set('Content-Type', 'application/json');
	
		return $response;
	}

}
